#include "LuaManager.h"
#include "LuaApi.h"
#include <unordered_map>


LuaManager::LuaManager()
{
}
LuaManager::~LuaManager()
{
	lua_close(m_LuaState);
}

void LuaManager::InitLua()
{
	m_LuaState = luaL_newstate();
	luaL_openlibs(m_LuaState);
	auto result =  luaL_dofile(m_LuaState, "./lua/main.lua");

	LuaAPI::InitRegLuaAPI(m_LuaState);

	lua_getglobal(m_LuaState, "OnInitLua");
	int id = 10;
	lua_pushinteger(m_LuaState, id);
	auto res = lua_pcall(m_LuaState, 1, 0, 0);
	if (res)
	{
		printf("do lua success~!\n");
	}
	lua_getglobal(m_LuaState, "OnExitLua");
	res = lua_pcall(m_LuaState, 0, 0, 0);
	if (res)
	{
		printf("do lua success~!\n");
	}
}

bool LuaManager::CallLua(const char* fnName, ...)
{
	lua_getglobal(m_LuaState, fnName);
	//lua_pushinteger(m_LuaState, ...);
	auto res = lua_pcall(m_LuaState, 1, 0, 0);
	if (res)
	{
		printf("do lua %s success~!\n", fnName);
		return true;
	}

    return false;
}
void LuaManager::GetStaticVar(lua_State* L)
{
	//auto& table =  LuaManager::GetInstance()->GetVarTable();
	m_VarTable["testData"] = 6;


}

void LuaManager::InitLuaAPI()
{
	LuaAPI::InitRegLuaAPI(m_LuaState);
}
